package top.oneit.jdownload.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lingkang
 * @date 2021/12/12
 */
public class BaseUtils {
    private static Map<String, Long> exists = new HashMap<>();
    private static final int minTime = 618;
    private static long lastClickTime = 0;

    /**
     * 重复点击检查，检查通过返回 true，，允许返回true
     */
    public static boolean repeatClickChecking(long minTime) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastClickTime > minTime) {
            lastClickTime = currentTime;
            return true;
        }
        return false;
    }

    /**
     * 重复点击检查，检查通过返回 true，，允许返回true
     */
    public static boolean repeatClickChecking() {
        return repeatClickChecking(minTime);
    }

    /**
     * 重复点击检查，检查通过返回 true，，允许返回true
     */
    public static boolean repeatClickChecking(String key) {
        Long aLong = exists.get(key);
        if (aLong != null && aLong + minTime > System.currentTimeMillis()) {
            return true;
        }
        exists.put(key, System.currentTimeMillis());
        return false;
    }
}
